<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2021/10/22
 * Time: 10:50
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

declare(strict_types=1);

namespace app\admin\controller;

use app\BaseController;
use app\common\lib\Show;
use app\common\lib\Status;
use app\common\lib\Time;
use app\common\traits\JumpTrait;
use think\Exception;
use think\facade\View;

class Base extends BaseController
{
    protected string $layout = 'layout/default';
    protected $indexWhere = [];
    protected $indexOrderBy = 'id desc';
    protected $indexField = '*';

    protected function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
        $this->layout && $this->app->view->engine()->layout($this->layout);
        $result = [
            "normal" => Status::status_normal,
            "error" => Status::status_error,
            "sql_normal" => Status::mysql_normal,
        ];
        $monthTime = Time::month();
        $this->assign('start_time', date('Y-m-d H:i:s', $monthTime[0]));
        $this->assign('end_time', date('Y-m-d H:i:s', $monthTime[1]));

        $this->assign('result', $result);
    }

    /**
     * 返回
     * @param string $template
     * @param array $vars
     * @return string
     */
    protected function fetch(string $template = '', array $vars = [])
    {
        return View::fetch($template, $vars);
    }

    /**
     * @param $name
     * @param null $value
     * @return \think\View
     */
    protected function assign($name, $value = null)
    {
        return View::assign($name, $value);
    }

    /**
     * @return string|\think\response\Json
     */
    public function index()
    {

        if ($this->request->isAjax()) {
            $limit = $this->request->param('limit', 10, 'intval');
            $this->indexWhere[] = [
                ['status','<>',Status::mysql_delete]
            ];
            try {
                $lists = $this->busObj->getIndexLimitPage($limit, $this->indexWhere, $this->indexOrderBy, $this->indexField);
            } catch (\Exception $e) {
                return Show::json_show(Status::status_error, "发生错误");
            }
            return Show::json_show(Status::status_normal, "ok", $lists['data'], $lists['total']);
        }
        return $this->fetch();
    }

    public function add()
    {
        return $this->fetch();
    }

    /**
     * 添加方法
     * @param array $data
     * @return \think\response\Json
     */
    public function save(array $data = [])
    {
        if (!$this->request->isPost()) {
            return Show::json_show(Status::status_error, "请求格式错误");
        }
        $param = $this->request->param();
        if (!empty($data)) {
            $param = array_merge($param, $data);
        }
        try {
            $res = $this->busObj->add($param);
        } catch (\Exception $e) {
            return Show::json_show(Status::status_error, $e->getMessage());
        }
        if ($res) {
            return Show::json_show(Status::status_normal, "ok");
        } else {
            return Show::json_show(Status::status_error, "操作失败");
        }
    }

    public function edit()
    {
        $eid = $this->request->param('eid', 0, 'intval');
        if ($eid == 0) {
            throw new Exception("内部错误");
        }
        $edit = $this->busObj->getBaseById($eid);
        if (!$edit) {
            throw new Exception("信息不存在");
        }
        return $this->fetch('', [
            "edit" => $edit
        ]);
    }

    public function delete()
    {
        $eid = $this->request->param('eid', 0, 'intval');
        if ($eid == 0) {
            return Show::json_show(Status::status_error, "内部错误");
        }
        try {
            $res = $this->busObj->updateByData((int)$eid, [
                "status" => Status::mysql_delete,
                "update_time"=>time()
            ]);
        } catch (\Exception $e) {
            return Show::json_show(Status::status_error, $e->getMessage());
        }
        if ($res) {
            return Show::json_show(Status::status_normal, "ok");
        } else {
            return Show::json_show(Status::status_error, "操作失败");
        }
    }
}
